<template>
	<view>
		<view style="display: flex;flex-direction: column; margin: 50rpx auto 20rpx auto;width: 80%;background-color: white;border-radius: 20rpx;box-shadow: 1px 1px 5px #c2c2c2;padding: 30rpx;">
			<view style="display: flex; width: 100%;flex-direction: row;align-items: center;">
				手机号
				<uni-easyinput style="width: 100%; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx; " v-model="tel" type="number"
					placeholder="请输入手机号" />
			</view>
			<view style="display: flex; width: 100%;flex-direction: row;align-items: center;margin-top: 30rpx;">
				验证码
				<uni-easyinput style="width: 100%; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx;margin-right: 20rpx; " v-model="yanzhengma" type="text"
					placeholder="请输入验证码" />
				<button style="background-color: #247DFD;color: white;padding: 5rpx 10rpx;width: 160rpx;" size='mini' @click="duanxin">{{smstext}}</button>
			</view>	
			<view style="display: flex; width: 100%;flex-direction: row;align-items: center;margin-top: 30rpx;">
				密码
				<uni-easyinput style="width: 100%; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx; " v-model="mima" type="password"
					placeholder="请输入密码" />
			</view>
			<view style="display: flex; width: 100%;flex-direction: row;align-items: center;margin-top: 30rpx;">
				重复密码
				<uni-easyinput style="width: 100%; border-radius: 15rpx;height: 80rpx;margin-left: 25rpx; " v-model="remima" type="password"
					placeholder="再输入一次密码" />
			</view>	
		</view>
		<view @click="zhuce" style="display: flex;width: 40%;margin: 100rpx auto 50rpx auto;background-color: blue;border-radius: 20rpx;color: white;font-weight: bold;padding: 20rpx;align-items: center;justify-content: center;">
			密码确认
		</view>
	</view>
</template>

<script>
	import md5 from '@/util/md5';
	export default {
		data() {
			return {
				code:'',
				smstext:'获取验证码',
				yanzhengma:'',
				tel:'',
				mima:'',
				remima:'',
			}
		},
		onLoad() {
		},
		methods: {
			zhuce(){
				if(this.mima!==this.remima){
					this.mima=this.remima=''
					return uni.showToast({
						title: '两次密码输入不一致，请重新输入',
						icon: 'none',
						mask: true
					})
				}
				if(this.mima.length<=5){
					this.mima=this.remima=''
					return uni.showToast({
						title: '密码位数应大于等于6位！',
						icon: 'none',
						mask: true
					})
				}
				if(Number(this.yanzhengma)!==Number(this.code)){
					this.yanzhengma=''
					return uni.showToast({
						title: '验证码错误！',
						icon: 'none',
						mask: true
					})
				}
				uniCloud.callFunction({
					name:"funshare",
					data:{
						leixing:13,//为3时，修改密码
						mima:md5(this.mima),
						tel:this.tel,	
					}
				}).then(res=>{
					console.log("111111",res)
					uni.navigateTo({
						url: '/pages/denglu/denglu'
					})
				}).catch((err) => {
					console.log(err)
					uni.showToast({
						title: '注册失败，请检查本手机号是否已注册！',
						icon: 'none',
						duration:3000
					})
				})
			},
			duanxin(){
				if (this.smstext !== '获取验证码') {
					return;
				}
				// 检查手机号是否正确
				let myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
				if (!myreg.test(this.tel)) {
					return uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						mask: true
					})
				}
				//查询手机号信息
				uniCloud.callFunction({
					name:"funshare",
					data:{
						leixing:14,//为14时，查询是否有本手机号
						tel:this.tel,
					}
				}).then(res=>{
					console.log('-0',res)
					if(!res.result.data.length){
						return uni.showToast({
							title: '手机号错误，请核实手机号',
							icon: 'none',
							mask: true
						})
					}else{
						//上面两个都没问题，执行发送短信
						let code=Math.floor(Math.random() *9000) + 1000
						this.code=code
						this.duanxin1()
					}
				})
				
			},
			duanxin1(){
				uniCloud.callFunction({
					name:"duanxin",
					data:{
						tel:this.tel,
						code:this.code
					}
				}).then(res=>{
					console.log("123",res)
					let num = 60;
					this.smstext = num;
					let timer = setInterval(() => {
						num -= 1;
						this.smstext = num;
						if (num == 1) {
							this.smstext = '获取验证码'
							clearInterval(timer)
						}
					}, 1000);
					// uni.showToast({
					// 	title: '发布完成',
					// 	icon: 'none',
					// 	duration: 3000
					// })
				})
			}
		}
	}
</script>

<style>

</style>
